package close.mazegame.entity.mob.behaviour;

import java.awt.Point;
import java.util.ArrayList;
import java.util.Random;

import com.badlogic.gdx.math.Vector2;

import close.mazegame.entity.mob.AITank;
import close.mazegame.tile.Tile;

public class PatrolBehaviour extends TargetBehaviour {

	private static Random RANDOM = new Random();
	
	// The last tile we were on, to prevent u-turns
	public Point lastUpdate; //Tile we were on last update
	public Point lastTile;  //Tile we were on previous to this one

	public PatrolBehaviour(AITank mob) {
		super(mob);
		lastUpdate = new Point((int)(mob.getX()), (int)(mob.getY()));
		lastTile = new Point((int)(mob.getX()), (int)(mob.getY()));
	}

	@Override
	public void setTarget() {
		Point current = new Point((int)(mob.getX()), (int)(mob.getY()));
		if(!current.equals(lastUpdate)){
			lastTile = new Point(lastUpdate);
			lastUpdate = new Point(current);
		}
		
		ArrayList<Point> neighbours = mob.getPathfinder().getNeighbours(current, true);
		if (neighbours.size() == 0) {
			return;
		} else if (neighbours.size() == 1) {
			target = new Vector2(neighbours.get(0).x, neighbours.get(0).y);
		} else {
			int n = RANDOM.nextInt(neighbours.size());
			while(neighbours.get(n).equals(lastTile)){
				n = RANDOM.nextInt(neighbours.size());
			}
			target = new Vector2(neighbours.get(n).x, neighbours.get(n).y);
		}
		target.x += Tile.SIZE / 2.0;
		target.y += Tile.SIZE / 2.0;
	}

}
